/*
 * Copyright 2006 Berry Crawford
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.judo.database;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;

import org.judo.database.exceptions.ORMException;

public class TypeTranslator
{
	public static Class getJavaType(int type) 
		throws ORMException
	{
		Class classType = null;
		
		if (type == Types.VARCHAR)
			return String.class;
		else if (type == Types.BIGINT)
			return Long.class;
		else if (type == Types.BINARY)
			return Object.class;
		else if (type == Types.BIT)
			return Boolean.class;
		else if (type == Types.BOOLEAN)
			return Boolean.class;
		else if (type == Types.CHAR)
			return Character.class;
		else if (type == Types.DATE)
			return Date.class;
		else if (type == Types.DECIMAL)
			return Double.class;
		else if (type == Types.DOUBLE)
			return Double.class;
		else if (type == Types.FLOAT)
			return Float.class;
		else if (type == Types.INTEGER)
			return Integer.class;
		else if (type == Types.LONGVARCHAR)
			return String.class;
		else if (type == Types.NUMERIC)
			return BigDecimal.class;
		else if (type == Types.REAL)
			return Float.class;
		else if (type == Types.SMALLINT)
			return Short.class;
		else if (type == Types.TIME)
			return Time.class;
		else if (type == Types.TIMESTAMP)
			return Timestamp.class;
		else if (type == Types.TINYINT)
			return Short.class;
		else if (type == Types.VARCHAR)
			return String.class;
		else
			throw new ORMException("SQL Datatype not translated: "+type);
	}
	
	public static int getSQLType(Class clazz)
	{	
		String type = clazz.getCanonicalName();
	
		if (type.equals("java.lang.String"))
			return Types.VARCHAR;
		else if (type.equals("java.lang.Short"))
			return Types.TINYINT;
		else if (type.equals("java.lang.Integer"))
			return Types.INTEGER;
		else if (type.equals("java.util.Date"))
			return Types.DATE;
		else if (type.equals("java.lang.Long"))
			return Types.BIGINT;
		else if (type.equals("java.lang.Float"))
			return Types.REAL;
		else if (type.equals("java.lang.Double"))
			return Types.DOUBLE;
		else if (type.equals("java.lang.Boolean"))
			return Types.BIT;
		else if (type.equals("java.sql.Date"))
			return Types.DATE;
		else if (type.equals("java.lang.Character"))
			return Types.CHAR;
		else if (type.equals("int"))
			return Types.INTEGER;
		else if (type.equals("short"))
			return Types.TINYINT;
		else if (type.equals("long"))
			return Types.BIGINT;
		else if (type.equals("double"))
			return Types.DOUBLE;
		else if (type.equals("float"))
			return Types.REAL;
		else if (type.equals("boolean"))
			return Types.BIT;
		else if (type.equals("char"))
			return Types.CHAR;
		else
			return Types.BINARY;
	}
}
